<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="shortcut icon" href="${ bp }/static/img/icon.png" type="image/x-icon">
    <link rel="stylesheet" href="${ bp }/static/layui/css/layui.css">
</head>

<body>
<div class="layui-container">
    <form class="layui-form layui-col-md4 layui-col-md-offset3">
        <div class="layui-form-item">
            <label class="layui-form-label">昵称</label>
            <div class="layui-input-block">
                <input type="text" name="nickname" value="${ onlineAdmin.nickname }" readonly lay-verify="required"
                       lay-reqText="昵称不能为空" autocomplete="off" placeholder="请输入昵称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" value="${ onlineAdmin.username }" readonly lay-verify="required"
                       lay-reqText="用户名不能为空" autocomplete="off" placeholder="请输入用户名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">最后登录时间</label>
            <div class="layui-input-block">
                <input type="text" value="${ onlineAdmin.last_login_time }" readonly autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="updateBtn">修改信息</button>
                <button type="button" class="layui-btn layui-btn-warm layui-hide" lay-submit lay-filter="modify"
                        id="saveBtn">保存修改
                </button>
                <button type="button" class="layui-btn layui-btn-danger layui-hide" id="cancelBtn">取消修改</button>
            </div>
        </div>
    </form>

</div>

<script src="${ bp }/static/layui/layui.all.js"></script>
<script>
    layui.use(['form', 'layer', 'jquery'], function () {
        let form = layui.form;
        let layer = layui.layer;
        let $ = layui.jquery;

        // 默认值
        let defaultNickname = $('[name="nickname"]').val();
        let defaultUsername = $('[name="username"]').val();

        // 点击“修改信息”按钮
        $('#updateBtn').click(function () {
            $(this).addClass('layui-hide');
            $('#saveBtn').removeClass('layui-hide');
            $('#cancelBtn').removeClass('layui-hide');

            $('[name="nickname"]').removeAttr('readonly');
            $('[name="username"]').removeAttr('readonly');
        });

        // 点击“取消修改”按钮
        $('#cancelBtn').click(function () {
            $(this).addClass('layui-hide');
            $('#saveBtn').addClass('layui-hide');
            $('#updateBtn').removeClass('layui-hide');

            $('[name="nickname"]').attr('readonly', '');
            $('[name="username"]').attr('readonly', '');

            $('[name="nickname"]').val(defaultNickname);
            $('[name="username"]').val(defaultUsername);
        });

        // 监听表单提交
        form.on('submit(modify)', function (data) {
            // 调用ajax提交修改数据
            $.ajax({
                url: '${ bp }/admin?method=info',
                type: 'post',
                data: data.field,
                dataType: 'json',
                success: (result) => {
                    if (result.code === 0) {
                        layer.msg('修改成功！将为你刷新页面！', {
                            time: 1000,
                            icon: 1
                        }, function () {
                            // 刷新顶级窗口
                            top.location.reload();
                        });
                    } else {
                        layer.msg(result.msg, {
                            time: 1000,
                            icon: 2
                        });
                    }
                },
                error: () => {
                    console.log('请求失败！');
                }
            });
        });
    });
</script>

</body>
</html>
